﻿
; (function (window, undefined, $) {
    var submiting = false;
    $('#frmEdit').validator({
        rules: {
            
        },
        fields: {
            
        },
        valid: function (form) {
            if (submiting) {
                return;
            }
            submiting = true;
            FormSubmit(form, function(res) {
                if (res.Status > 0) {
                    alert("操作成功！");
                    window.location.href = "/Admin/T_Project/Project";
                } else {
                    alert(res.Message);
                }
                submiting = false;
            });
        }
    });

    $("#btnReturn").click(function (e) {
        window.location.href = "/Admin/T_Project/Project";
    });
    
    $("#btnClose").click(function (e) {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    $("#btnLoan").click(function (e) {
        if ($('#LoanUser').val() != "") {
            $.post(options.Loan , { id: $('#id').val(), name: $('#LoanUser').val() },
              function (res) {
                  if (res > 0) {
                      alert("操作成功！");
                      window.location.href = "/Admin/T_Project/Project";
                  }
                  else {
                      alert("操作失败！");
                  }
              },
              "json"
        );
        }
        else {
            alert('借款人不能为空！');
        }
    });
    
    //审核通过
    $("#btnAudit").click(function (e) {
            $.post(options.Audit, { id: $('#id').val(), name: $('#AuditUser').val(), status: 2, Audit: $('#AuditExplain').val() },
              function (res) {
                  if (res > 0) {
                      alert("操作成功！");
                      window.location.href = "/Admin/T_Project/ProjectAuditList";
                  }
                  else {
                      alert("操作失败！");
                  }
              },
              "json"
        );
    });

    //审核不通过
    $("#btnBack").click(function (e) {
            $.post(options.Audit, { id: $('#id').val(), name: $('#AuditUser').val(), status: 1, Audit: $('#AuditExplain').val() },
              function (res) {
                  if (res > 0) {
                      alert("操作成功！");
                      window.location.href = "/Admin/T_Project/Project";
                  }
                  else {
                      alert("操作失败！");
                  }
              },
              "json"
        );
    });


    $("#btnRept").click(function (e) {
            if ($('#RepaymentUser').val() != "") {
                $.post(options.Rept, { id: $('#id').val(), name: $('#RepaymentUser').val() },
                  function (res) {
                      if (res > 0) {
                          alert("操作成功！");
                          window.location.href = "/Admin/T_Project/Project";
                      }
                      else {
                          alert("操作失败！");
                      }
                  },
                  "json"
            );
            }
            else {
                alert('还款人不能为空！');
            }
    });
    
    //打开子窗口并且查询出公司名字
    util.GetEnterpriseName = function (id) {
        $.layer({
            type: 2,
            shade: [0],
            fix: false,
            title: '企业信息',
            maxmin: false,
            iframe: { src: options.companyUrl },
            area: ['800px', '440px'],
            close: function (index) {
                //子窗口返回值 layer.getChildFrame('#name', index).val()
                //layer.msg('您获得了子窗口标记：' + layer.getChildFrame('#name', index).val(), 3, 1);
            },
            end: function (index) {
                if ($('#hideEName').val()!="") {
                    $('#lbEnterpriseName').html($('#hideEName').val());
                }
            }
        });

    };
    util.GetRates = function(id, num) {
        num = num.replace(/[^0-9|\.]/g, '');
        if (num > 1) {
            num = 1;
        }
        $('#' + id).val(num);
    };

    util.GetNum = function(id, num) {
        num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
        if (num > 100) {
            num = 100;
        }
        var no = changeTwoDecimal(parseFloat(num));
        if (!no) {
            $('#' + id).val('');
        } else {
            $('#' + id).val(no.toFixed(2));
        }
    };

    util.GetNums = function(id, num) {
        num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
        $('#' + id).val(num);
    };

    util.getFloatStr = function (id, num) {
        num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
        var no = changeTwoDecimal(parseFloat(num));
        if (!no) {
            $('#' + id).val('');
        }
        else {
            $('#' + id).val(no.toFixed(2));
        }
    };

    function changeTwoDecimal(x) {
        if (isNaN(x)) {
            return false;
        }
        f_x = Math.round(x * 100) / 100;
        return f_x;
    }


    util.Delete = function (id) {
        if (!confirm("您确定要执行删除操作吗？")) {
            return false;
        }
        $.post(options.deleteUrl + "/" + id, { id: id },
              function (res) {
                  if (res.Status > 0) {
                      window.location.reload();
                  }
                  else {
                      alert("操作失败！");
                  }
              },
              "json"
        );
    };

})(window, undefined, jQuery);

$(document).ready(function() {
    var Request = new Object();
    Request = GetRequest();
    if (Request["status"] != null) {
        if (Request["status"] == 1) { //添加项目
            $('#btnAudit').css('display', 'none');
            $('#RepaymentDetailTitle').css('display', 'none');
            $('#RepaymentDetail').css('display', 'none');
        }
        if (Request["status"] == 2) { //审核项目
            $('#btnSave').css('display', 'none');
            $('#RepaymentDetailTitle').css('display', 'none');
            $('#RepaymentDetail').css('display', 'none');
        }
        if (Request["status"] == 3) { //查看项目
            $('#btnAudit').css('display', 'none');
            $('#btnSave').css('display', 'none');

        }
    }
});

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}